/**
 * 角色模块授权 - 页面
 * @author yuan_rz
 * @date 2019/01/07 11:26
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            treeData: []
        },
        roleId
    ;

    var roleModules = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'role_modules',
            }, win);
        },
        getRoleModules: function () {
            app.ajax({
                xurl: 'sys/role/getRoleModulesList',
                xdata: {
                    roleId: roleId
                },
                cb: function (result) {
                    if (result.ok) {
                        self.vues['page'].$refs.tree.setCheckedKeys(result.data.data,true);
                    }
                }
            });
        },
        saveRoleModules: function () {
            var selectedNodes = self.vues['page'].$refs.tree.getCheckedNodes(false,true);
            var _xdata = [];
            selectedNodes.forEach(function (v) {
                var _temp = app.fields_('sys_role_module');
                _temp.ROLE_ID = roleId;
                _temp.MODULE_ID = v.id;
                _temp.GRANT_TYPE = '1';
                _xdata.push(_temp);
            });
            app.ajax({
                xurl: 'sys/role/saveRoleModulesList',
                xdata: _xdata,
                loading: true,
                toJsonStr: true,
                okmsg: '模块授权成功',
                cb: function (result) {
                    if (result.ok) {
                        layer.close(layer.index);
                    }
                }
            });
        },
        _call: function (obj) {
            roleId = obj.roleId;
            vueData.treeData = obj.treeData;
            self.getRoleModules();
        },
        _ready: function () {
            this._super({
                vue: [{
                    el: '#role_modules',
                    id : 'page',
                    data: vueData,
                    methods: {
                        cancel: function () {
                            layer.close(layer.index);
                        },
                        sure: function () {
                            self.saveRoleModules();
                        }
                    },
                    watch: {},
                    created: function () {
                        self._setEleIsReady('role_modules');
                    }
                }]
            });
        },
        _lazy: function () {
            this._super();
        }
    });

    return new roleModules();

});